var express = require('express');
var router = express.Router();
let { OperationAndMaintenanceWorkOrderModel,CommodityManagementModel,OperationWorkOrderModel,TypeOfMerchandiseModel } = require('../db/yu')

/* 运维 */
router.get('/yunwei',  async (req, res, next)=> {

  let WorkOrderNumber = req.query.WorkOrderNumber
  let WorkOrderStatus = req.query.WorkOrderStatus
  let page =  req.query.page || 1
  let size = req.query.size || 5
  let data = []
  try{
    if(WorkOrderNumber && WorkOrderStatus){
      data = await OperationAndMaintenanceWorkOrderModel.find({WorkOrderNumber:WorkOrderNumber,WorkOrderStatus:WorkOrderStatus}).skip((page-1)*size).limit(size)
    }else if(WorkOrderNumber){
      data = await OperationAndMaintenanceWorkOrderModel.find({WorkOrderNumber:WorkOrderNumber}).skip((page-1)*size).limit(size)
    }else if(WorkOrderStatus){
      data = await OperationAndMaintenanceWorkOrderModel.find({WorkOrderStatus:WorkOrderStatus}).skip((page-1)*size).limit(size)
    }else{
      data = await OperationAndMaintenanceWorkOrderModel.find().skip((page-1)*size).limit(size)
    }
    res.send({
      coed:200,
      data,
    })
  }catch (error){
    res.send({code:500})
  }
});

router.post('/yunwei', async(req, res, next)=>{
  try {
    await OperationAndMaintenanceWorkOrderModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
router.put('/yunwei', async(req, res, next)=>{
  try {
    await OperationAndMaintenanceWorkOrderModel.updateOne({_id:req.body.id},req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
router.delete('/yunwei', async(req, res, next)=>{
  try {
    console.log(req.query);
    await OperationAndMaintenanceWorkOrderModel.deleteOne({_id:req.query.id})
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});









router.get('/yunying',  async (req, res, next)=> {
  let WorkOrderNumber = req.query.WorkOrderNumber
  let WorkOrderStatus = req.query.WorkOrderStatus
  let page =  req.query.page || 1
  let size = req.query.size || 5
  let data = []
  try{
    if(WorkOrderNumber && WorkOrderStatus){
      data = await OperationWorkOrderModel.find({WorkOrderNumber:WorkOrderNumber,WorkOrderStatus:WorkOrderStatus}).skip((page-1)*size).limit(size)
    }else if(WorkOrderNumber){
      data = await OperationWorkOrderModel.find({WorkOrderNumber:WorkOrderNumber}).skip((page-1)*size).limit(size)
    }else if(WorkOrderStatus){
      data = await OperationWorkOrderModel.find({WorkOrderStatus:WorkOrderStatus}).skip((page-1)*size).limit(size)
    }else{
      data = await OperationWorkOrderModel.find().skip((page-1)*size).limit(size)
    }
    res.send({
      coed:200,
      data
    })
  }catch (error){
    res.send({code:500})
  }
});
router.post('/yunying', async(req, res, next)=>{
  console.log(req.body);
  try {
    await OperationWorkOrderModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});

router.put('/yunying', async(req, res, next)=>{
  try {
    await OperationWorkOrderModel.updateOne({_id:req.body.id},req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
router.delete('/yunying', async(req, res, next)=>{
  try {
    await OperationWorkOrderModel.deleteOne({_id:req.query.id})
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});




router.get('/spgl',  async (req, res, next)=> {
  let commodityName = req.query.commodityName
  let page =  req.query.page || 1
  let size = req.query.size || 5
  let data = []
  console.log(commodityName);
  try{
    if(commodityName){
      
      data = await CommodityManagementModel.find({commodityName:RegExp(commodityName)}).skip((page-1)*size).limit(size)
    }else{
      
      data = await CommodityManagementModel.find().skip((page-1)*size).limit(size)
      console.log(data);
    }
    
    res.send({
      coed:200,
      data
    })
  }catch (error){
    res.send({code:500})
  }
});

router.post('/spgl', async(req, res, next)=>{
  console.log(req.body);
  try {
    await CommodityManagementModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});

router.put('/spgl', async(req, res, next)=>{
  try {
    await CommodityManagementModel.updateOne({_id:req.body.id},req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
router.delete('/spgl', async(req, res, next)=>{
  try {

    await CommodityManagementModel.deleteOne({_id:req.query.id})
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});


router.get('/splx',  async (req, res, next)=> {
  let commodityName = RegExp(req.query.commodityName)
  let page =  req.query.page || 1
  let size = req.query.size || 5
  try{
    let data = []
    if(commodityName){
      data = await TypeOfMerchandiseModel.find({commodityName:commodityName}).skip((page-1)*size).limit(size)
    }else{
      data = await TypeOfMerchandiseModel.find().skip((page-1)*size).limit(size)

    }

    res.send({
      coed:200,
      data
    })
  }catch (error){
    res.send({code:500})
  }
});


router.post('/splx', async(req, res, next)=>{
  console.log(req.body);
  try {
    await TypeOfMerchandiseModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});

router.put('/splx', async(req, res, next)=>{
  try {
    await TypeOfMerchandiseModel.updateOne({_id:req.body.id},req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
router.delete('/splx', async(req, res, next)=>{
  try {
    await TypeOfMerchandiseModel.deleteOne({_id:req.query.id})
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});

module.exports = router;
